package epam.com.threads.application;

import epam.com.threads.entities.Port;
import epam.com.threads.entities.Ship;
import epam.com.threads.entities.Wharf;
import epam.com.threads.util.PropertiesReader;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Yelena_M
 * Date: 05.09.13
 * Time: 18:38
 * To change this template use File | Settings | File Templates.
 */
public class Application {

    private final static Logger LOGGER = Logger.getLogger(Application.class);

    public static void main(String[] args) {

        PropertiesReader propertiesReader = new PropertiesReader();

        Port.open(); //аля инициализация
        Port port = Port.getInstance();
        int shipAmount = Integer.parseInt(propertiesReader.getProperties("data.shipAmount"));

        port.setCapacityPort(Integer.parseInt(propertiesReader.getProperties("data.capacityPort")));

        Wharf wharf;
        for (int i = 0; i < port.getCapacityPort(); i++) {
            port.createWharf(wharf = new Wharf(i));
            LOGGER.info("Added wharf " + wharf.getWharfNumber());
        }

        List<Ship> ships = new ArrayList<Ship>();
        List<Thread> threadsShip = new ArrayList<Thread>();

        Ship ship;
        for (int i = 0; i < shipAmount; i++) {
            ships.add(ship = new Ship("ship" + i));
            LOGGER.info("Added ship " + ship.getShipName());
            threadsShip.add(new Thread(ship));
        }

        for (int i = 0; i < shipAmount; i++) {
            threadsShip.get(i).start();
            LOGGER.info("Thread's started " + i);
        }

        for (int i = 0; i < shipAmount; i++) {
            try {
                threadsShip.get(i).join();
                LOGGER.info("Thread's joined");
            } catch (InterruptedException e) {
                LOGGER.error(e);
            }
        }

        LOGGER.info("The End :)");
    }

}
